const jwt = require("jsonwebtoken");
const userData = [
	{
		username: "kkk",
		password: "123",
	},
	{
		username: "admin",
		password: "123",
	},
];
const bodyParser = require("body-parser");
const mock = require("mockjs");
const { list } = mock.mock({
	"list|200": [
		{
			"type|1": ["奥迪", 2, 3, 4, 5, 6, 7],
			title: "@ctitle(10,20)",
			image: "@image(100x100,@color)",
			id: "@id",
			desc: "@cword(20,25)",
			create_time: Date.now(),
			"status|1": [1, 2, 3, 4], // 1 待发货 2 已发货 3 待签收 4 订单完成
		},
	],
});
module.exports = {
	devServer: {
		before(app) {
			// 模拟接口
			// 登录
			app.use(bodyParser.json());
			app.post("/login", (request, response) => {
				const { username, password } = request.body;
				const isHave = userData.some(
					item =>
						item.username === username && item.password === password
				);
				// some 一真则真  every 一假则假
				// filter map forEach find findIndex
				// reduce
				if (isHave) {
					// 登录成功返回一个token
					response.send({
						success: true,
						token: jwt.sign({ username }, "1905A"),
					});
				} else {
					response.send({
						success: false,
						message: "请检查账号密码",
					});
				}
			});
			// 获取用户信息
			// /info?token=xxxxx
			app.get("/info", (request, response) => {
				const token = request.headers.authorization;
				if (!token) {
					response.send({
						success: false,
						message: "请先登录",
					});
				}
				const data = jwt.verify(token, "1905A"); // 解析token
				if (data.username === "admin") {
					response.send({
						success: true,
						role: "admin",
						name: "admin",
					});
				} else {
					response.send({
						success: true,
						role: "editor",
						name: "editor",
					});
				}
			});
			// 数据的增删改查 都交给接口来处理
			// 前端只负责 那接口数据 和 掉接口修改数据
			// 订单列表 = 分页 模糊查询 排序 按照日期搜索
			/**
			 * pageIndex 当前页
			 * pageSize 每页多少条
			 * sortBy  根据什么排序 {id, create_time}
			 * sortType  desc 倒叙 asc 正序
			 * start_date 开始时间
			 * end_date 结束时间
			 */
			// /list?id=1&name=kkk queryString
			app.get("/order/list", (request, response) => {
				const {
					pageIndex,
					pageSize,
					sortBy,
					sortType,
					start_date,
					end_date,
				} = request.query;
				// 1 10 => 0,10 slice
				// 2 10 => 10,20 slice
				// splice 会修改原数组
				// slice 不会修改原数组
				let cloneList = JSON.parse(JSON.stringify(list)); // 为了防止修改原数组  先克隆一个出来
				// 排序
				if (sortBy && sortType) {
					if (sortType === "asc") {
						cloneList.sort((a, b) => {
							return a[sortBy] - b[sortBy];
						});
					} else {
						cloneList.sort((a, b) => {
							return b[sortBy] - a[sortBy];
						});
					}
				}
				// 时间区间
				if (start_date && end_date) {
					cloneList = cloneList.filter(item => {
						return (
							item.create_time < end_date &&
							item.create_time > start_date
						);
					});
				}
				// 分页
				let newList = cloneList.slice(
					(pageIndex - 1) * pageSize,
					pageIndex * pageSize
				);

				response.send({
					success: true,
					data: {
						list: newList,
						total: cloneList.length,
					},
				});
			});
			// 创建订单
			app.post("/order/create", (request, response) => {
				const { title, desc, image } = request.body;
				const id = Date.now() + 111;
				list.push({
					title,
					desc,
					image,
					status: 1,
					id,
					create_time: Date.now(),
				});
				response.send({
					success: true,
					data: {
						id,
					},
				});
			});
			// 编辑订单
			// 删除订单
		},
		proxy: {
			"/wzs": {
				//这里最好有一个 /
				// http://localhost:8080/wsz/xxxx 表面
				// http://182.254.220.243:7001/xxxx 实际
				target: "http://182.254.220.243:7001", // 后台接口域名
				pathRewrite: {
					"^/wzs": "",
				},
			},
		},
	},
};
